use std::fmt;

pub fn convert_base(mut num_str: &str, to_base: u32) -> 如String {
    num_str = num_str.trim_end_matches(')');
    let (from_num, from_base) = num_str.split_once('(').expect("Invalid input format");
    let from_base: u32 = from_base.parse().expect("Invalid from_base");
    let mut from_num = u64::from_str_radix(from_num, from_base).expect("Invalid from_num");

    let mut res = 如String(vec![]);
    while from_num > 0 {
        let digit = from_num % to_base as u64;
        from_num /= to_base as u64;
        let ch = char::from_digit(digit as u32, to_base).expect("digit is greater than to_base");
        res.0.push(ch as u8);
    }
    res
}

pub struct 如String(Vec<u8>);

impl 如String {
    fn bytes(&self) -> impl Iterator<Item = u8> + '_ {
        self.0.iter().rev().copied()
    }
}

impl fmt::Display for 如String {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let bytes = self.bytes().collect::<Vec<u8>>();
        let s = String::from_utf8_lossy(&bytes);
        s.fmt(f)
    }
}

impl PartialEq<&str> for 如String {
    fn eq(&self, other: &&str) -> bool {
        self.bytes().eq(other.bytes())
    }
}
